package com.igroupes.ovineherd.model.attribute.text;

import cn.hutool.core.util.ReUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.IdCardStyle;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

/**
 * 身份证
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class IdCardModelData extends BasicAttribute {
    private static Integer LENGTH = 32;
    private static String REGEX = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$";
    public IdCardModelData() {
        super();
    }

    public Boolean verifyField(IdCardModelData json) {
        return super.verifyField(json);
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        IdCardModelData idCardModelData = (IdCardModelData)parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setLength(LENGTH);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }
    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        return super.getAttribute(modelDataJsonAttributeQuery);
    }

    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        List<FieldApiResponse> fieldApiResponses = super.verifyRequestData(modelAttributeEntity, value);
        if(value == null){
            return fieldApiResponses;
        }

        if(!ReUtil.isMatch(REGEX, value.toString())){
            throw new ModelException(String.format("%s格式错误",modelAttributeEntity.getName()));
        }

        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(value);
        fieldApiResponse.setIsStr(true);
        fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldApiResponses.add(fieldApiResponse);
        return fieldApiResponses;
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {
        JSONObject validations = new JSONObject();
        validations.set("matchRegexp", REGEX);
        JSONObject validationErrors = new JSONObject();
        validationErrors.set("matchRegexp", "请输入正确的身份证号码");
        return new JSONObject(new IdCardStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode()),
                validations, validationErrors
        ), false);
    }
}
